package com.sqfw.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PhoneNumberMasking{

    private static final Logger log = LoggerFactory.getLogger(PhoneNumberMasking.class);

    /**
     *  手机号码脱敏
     * @param phoneNumber 电话号码
     * @return 脱敏后手机号码
     */
    public static String maskPhoneNumber(String phoneNumber) {
        // 检查输入的电话号码是否合法
        if (phoneNumber == null || phoneNumber.length() != 11) {
            log.error("Invalid phone number:" + phoneNumber);
            return phoneNumber;
        }
        // 替换中间四位为 *
        StringBuilder maskedNumber = new StringBuilder(phoneNumber);
        maskedNumber.replace(3, 7, "****");

        return maskedNumber.toString();
    }

    public static void main(String[] args) {
        String phoneNumber = "12345678901";
        String maskedPhoneNumber = maskPhoneNumber(phoneNumber);

        System.out.println(maskedPhoneNumber);
    }
}
